package com.hl.im.code.generator;


import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

/**
 * @author huanglin
 * @apiNote
 * @date: 2023/10/16 17:36
 */
public class Main {

    private static final String TEMPLATE_PATH = "im-code-generator/src/main/java/com/hl/im/code/generator/templates";
    private static final String CLASS_PATH = "im-code-generator/src/main/java/com/hl/im/code/generator/output";

    public static void main(String[] args) {
        // 创建配置实例
        Configuration configuration = new Configuration();

        try {
            // 获取模板路径
            configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));
            // 创建数据模型
            Map<String, Object> dataMap = new HashMap<>();

            dataMap.put("classPath", "com.freemark.hello");
            dataMap.put("className", "User");
            dataMap.put("Id", "Id");
            dataMap.put("userName", "userName");
            dataMap.put("password", "password");

            Template template = configuration.getTemplate("test.ftl");
            File docFile = new File(CLASS_PATH + "\\" + "User.java");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            template.process(dataMap, out);

        } catch (IOException | TemplateException e) {
            throw new RuntimeException(e);
        }

    }
}
